package com.oliver.picturebackend.model.enums;

import cn.hutool.core.util.StrUtil;
import lombok.Getter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName PictureTypeEnum
 * @Description 图片类型的枚举
 * @Author snow
 * @Date 2024/12/15 18:36
 **/

@Getter
public enum PictureTypeEnum {
    //  "image/jpeg", "image/jpg", "image/png", "image/webp"
    JPG("image/jpeg", "jpeg"),
    JPEG("image/jpg", "jpg"),
    PNG("image/png", "png"),
    WEBP("image/webp", "webp"),
    ;

    /**
     * 图片类型
     */
    private String contentType;

    /**
     * 图片后缀
     */
    private String suffix;

    PictureTypeEnum(String contentType, String suffix) {
        this.contentType = contentType;
        this.suffix = suffix;
    }

    public static String getSuffixByContentType(String contentType) {
        if (StrUtil.isBlank(contentType)) {
            return null;
        }
        for (PictureTypeEnum pictureTypeEnum : PictureTypeEnum.values()) {
            if (pictureTypeEnum.contentType.equals(contentType)) {
                return pictureTypeEnum.suffix;
            }
        }
        //  默认返回jpg的后缀
        return JPG.suffix;
    }

    /**
     * 获取允许的图片类型列表
     * @return
     */
    public static List<String> getAllowContentTypes() {
        return Arrays.stream(PictureTypeEnum.values())
                .map(PictureTypeEnum::getContentType)
                .collect(Collectors.toList());
    }
}
